package com.sy.simpleegg.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sy.simpleegg.databinding.ItemSelectedBinding

class AppItemAdapter(val items: MutableList<String>, changedListener: OnItemClickListener?):
    RecyclerView.Adapter<AppItemAdapter.ViewHolder>() {
        
    private var onItemClickListener: OnItemClickListener? = null
    
    init {
        onItemClickListener = changedListener
    }
        
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppItemAdapter.ViewHolder {
        val binding = ItemSelectedBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return items.size
    }

    @SuppressLint("RecyclerView")
    override fun onBindViewHolder(holder: AppItemAdapter.ViewHolder, position: Int) {
        val names = items[position]
        holder.itemName.text = names
        holder.itemName.setOnClickListener {
            onItemClickListener?.onItemClick(position)
        }
    }

    private fun refreshTab(position: Int){

    }

    interface OnItemClickListener{
        fun onItemClick(position: Int)
    }
    

    inner class ViewHolder(binding: ItemSelectedBinding): RecyclerView.ViewHolder(binding.root){
        val itemName: TextView = binding.tvName
    }
}